package com.example.SpringDataJDBC;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootApplication
public class SpringDataJdbcApplication {

	private final Logger log = LoggerFactory.getLogger(SpringDataJdbcApplication.class);


	public static void main(String[] args) {
		SpringApplication.run(SpringDataJdbcApplication.class, args);
	}

	@Bean
	public CommandLineRunner runner(JdbcTemplate jdbcTemplate) {
		return args -> {
			log.info("Creating tables");
			jdbcTemplate.execute("drop table users if exists");
			jdbcTemplate.execute("create table users (id bigint generated by default as identity, name varchar(255), username varchar(255), primary key (id))");

			log.info("Insert Data");
			jdbcTemplate.execute("INSERT INTO users (name, username) VALUES " +
					" ('aaa', '@aaa');");
			jdbcTemplate.execute("INSERT INTO users (name, username) VALUES " +
					" ('bbb', '@bbb');");
		};
	}

}
